package com.example.administrator.runbook.fragments.community.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.runbook.R;
import com.example.administrator.runbook.fragments.community.adapter.TrendsDetailListAdapter;
import com.example.administrator.runbook.fragments.community.model.Comment;
import com.example.administrator.runbook.fragments.community.model.Tweets;
import com.example.administrator.runbook.fragments.community.utils.AsyncHttpClientHelper;
import com.example.administrator.runbook.fragments.community.utils.ParserData;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class DetailActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    private static final String TAG = "123456";
    private ListView listView;
    private List<Comment> list;
    private Tweets tweets;
    private TrendsDetailListAdapter adapter;
    private SwipeRefreshLayout refreshLayout;
    private String id;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    android.util.Log.i(TAG, "handleMessage: ");
                    if (msg.obj instanceof JSONObject) {
                        JSONObject obj = (JSONObject) msg.obj;
                        ParserData instance = ParserData.getInstance();
                        tweets = instance.getHotDetial(obj);
                        if (tweets != null) {
                            adapter.setTweets(tweets);
                            adapter.notifyDataSetChanged();
                            if (refreshLayout.isRefreshing()) {
                                refreshLayout.setRefreshing(false);
                            }
                        }
//                        List<Tweets> hot = instance.getHot(obj);
//                        list.addAll(hot);
//                        adapter.notifyDataSetChanged();
//
                    }
                    break;
                case 10:
                    android.util.Log.i(TAG, "handleMessage: ");
                    if (msg.obj instanceof JSONObject) {
                        JSONObject obj = (JSONObject) msg.obj;
                        ParserData instance = ParserData.getInstance();
                        List<Comment> comment = instance.getComment(obj);
                        list.clear();
                        list.addAll(comment);
                        if (tweets != null) {
                            adapter.setTweets(tweets);
                            adapter.notifyDataSetChanged();
                            if (refreshLayout.isRefreshing()) {
                                refreshLayout.setRefreshing(false);
                            }
                        }
                    }
                    break;
                case 1:
                    if (msg.obj instanceof String) {
                        String obj = (String) msg.obj;
                        Toast.makeText(DetailActivity.this, obj, Toast.LENGTH_SHORT).show();
                        adapter.notifyDataSetChanged();
                        if (refreshLayout.isRefreshing()) {
                            refreshLayout.setRefreshing(false);
                        }
                    }
                    break;
            }
            return true;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        listView = (ListView) findViewById(R.id.list_view_activity_detail);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_detail_activity);
        list = new ArrayList<>();
        tweets = new Tweets();
        adapter = new TrendsDetailListAdapter(this, list, tweets);
        initData();
        initListener();
        onRefresh();
    }

    private void initData() {

        listView.setAdapter(adapter);
        Bundle bundle = getIntent().getExtras();
        id = bundle.getString("id");
    }

    private void initListener() {

        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh() {

        if (id != null) {
            tweets = null;
            AsyncHttpClientHelper instance = AsyncHttpClientHelper.getInstance();
            instance.get(handler, "http://api.zhuishushenqi.com/user/twitter/" + id);
            instance.getComment(handler,
                    "http://api.zhuishushenqi.com/user/twitter/" + id + "/comments");
        } else {
            if (refreshLayout.isRefreshing()) {
                refreshLayout.setRefreshing(false);
            }
        }
    }
}
